package com.datahandlers;

import com.gui.helper.CustomTableModel;
import com.inventory.controllers.RoutesControllerRemote;
import com.inventory.entities.Route;
import com.util.ResourceLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Handlers the user interface actions for the route relation in the database.
 * @author sithum
 */
public class RoutesDataHandler implements DataHandler {

    private List<Route> routesList;
    private String[] tableHeaders = new String[]{ResourceLoader.getInstance().getProperty("route.column0"), ResourceLoader.getInstance().getProperty("route.column1"), new String()};
    private CustomTableModel model;
    private Object[][] tableData;
    private RoutesControllerRemote controller;
    private String[] routeNames;
    private Map<String, String> editRoutesMap;
    private List<Route> data;
    private static int recordCount = 0;

    public RoutesDataHandler(RoutesControllerRemote controller) {
        try {
            this.init(controller);
        } catch (Exception ex) {
            Logger.getLogger(RoutesDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void init(Object controller) throws Exception {
        this.controller = (RoutesControllerRemote) controller;
        this.routesList = new ArrayList<Route>();
        this.editRoutesMap = new HashMap<String, String>();
        this.processData();
        model = new CustomTableModel(tableHeaders, tableData);
        RoutesDataHandler.recordCount = this.controller.count();
    }

    /**
     * Add a record from the user interface to be saved to a java.lang.List.
     * @param java.lang.Object - route
     */
    public void addRecord(Object routeName) {
        Route route = new Route();
        route.setRouteId(++recordCount);
        route.setRouteName(routeName.toString());
        this.routesList.add(route);
    }

    public void saveData() throws Exception {
        controller.create(routesList);
        routesList.clear();
        this.processData();
        model.setTableData(this.getTableData());
    }

    public CustomTableModel getModel() {
        return model;
    }

    public boolean temporaryStorageEmpty() {
        return routesList.isEmpty();
    }

    public void addEditRecord(Object key, Object routeName) {
        try {
            Route r = controller.findRouteByName(key.toString());
            if (r != null) {
                r.setRouteName(routeName.toString());
                this.routesList.add(r);
            }
        } catch (Exception ex) {
            Logger.getLogger(RoutesDataHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void editData() throws Exception {
        controller.edit(routesList);
        routesList.clear();
        this.processData();
        model.setTableData(this.getTableData());
    }

    public boolean tempEditStorageEmpty() {
        return editRoutesMap.isEmpty();
    }

    /**
     * Process the data retrived from the database to display in a JTable.
     * @return java.lang.Object[][]
     * @throws java.lang.Exception
     */
    private void processData() throws Exception {
        setData(controller.findAll());
        this.processData(getData());
    }

    private void processData(List<Route> data) {
        Object[][] dataSet = new Object[data.size()][Route.class.getDeclaredFields().length];
        String[] names = new String[data.size()];
        int index = 0;
        for (Route route : data) {
            dataSet[index][0] = route.getRouteId();
            dataSet[index][1] = route.getRouteName();
            dataSet[index][2] = new Boolean(false);
            names[index] = route.getRouteName();
            index++;
        }
        this.setRouteNames(names);
        this.setTableData(dataSet);
    }

    public String[] getRouteNames() {
        return routeNames;
    }

    public void setRouteNames(String[] categoryNames) {
        this.routeNames = categoryNames;
    }

    public List<Route> getData() {
        return data;
    }

    public void setData(List<Route> data) {
        this.data = data;
    }

    public Map<String, String> getEditRoutesMap() {
        return editRoutesMap;
    }

    public void setEditRoutesMap(Map<String, String> editRoutesMap) {
        this.editRoutesMap = editRoutesMap;
    }

    public Object[][] getTableData() {
        return tableData;
    }

    public void setTableData(Object[][] tableData) {
        this.tableData = tableData;
    }

    public String[] getTableHeaders() {
        return tableHeaders;
    }

    public void setTableHeaders(String[] tableHeaders) {
        this.tableHeaders = tableHeaders;
    }
}
